package com.yauchu.kegra;

import com.yauchu.kegra.graph.GraphNode;

import android.content.ClipData;
import android.content.ClipDescription;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.Toast;

public class myDragEventListener implements OnDragListener {
		
		//private View mainView;
		//private int what;
		//private GraphContainer gr;
		private GraphNode node;
	
	    // This is the method that the system calls when it dispatches a drag event to the
	    // listener.
	    public myDragEventListener(GraphNode node)
	    {
	    	//this.mainView=mainV;
	    	//this.what=what;
	    	//this.gr=gr;
	    	this.node=node;
	    	
	    }
		
		public boolean onDrag(View v, DragEvent event) {

	        // Defines a variable to store the action type for the incoming event
	        final int action = event.getAction();

	        // Handles each of the expected events
	        switch(action) {

	            case DragEvent.ACTION_DRAG_STARTED:
	            
	            

	                // Determines if this View can accept the dragged data
	            	{
	                if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

	                    // As an example of what your application might do,
	                    // applies a blue color tint to the View to indicate that it can accept
	                    // data.
	                    //v.setScaleX(2);
	                    
	                    // Invalidate the view to force a redraw in the new tint
	                    v.invalidate();

	                    // returns true to indicate that the View can accept the dragged data.
	                    return(true);

	                    } else {

	                    // Returns false. During the current drag and drop operation, this View will
	                    // not receive events again until ACTION_DRAG_ENDED is sent.
	                    return(false);

	                    }}
	            	
	                
	                //break;
	                

	            case DragEvent.ACTION_DRAG_ENTERED: {

	                // Applies a green tint to the View. Return true; the return value is ignored.

	                //v.setScaleY(2);

	                // Invalidate the view to force a redraw in the new tint
	                v.invalidate();

	                return(true);
	            }
	                //break;
	                

	                case DragEvent.ACTION_DRAG_LOCATION:

	                // Ignore the event
	                    return(true);

	                //break;

	                case DragEvent.ACTION_DRAG_EXITED:
	                {

	                    // Re-sets the color tint to blue. Returns true; the return value is ignored.
	                    v.setScaleX(1);

	                    // Invalidate the view to force a redraw in the new tint
	                    v.invalidate();

	                    return(true);
	                }
	                //break;

	                case DragEvent.ACTION_DROP:
	                {
	                    // Gets the item containing the dragged data
	                    ClipData.Item item = event.getClipData().getItemAt(0);

	                    // Gets the text data from the item.
	                    CharSequence dragData = item.getText();

	                    // Displays a message containing the dragged data.
	                    //((EditText) v).setText("Dropped");
	                    //Toast.makeText(this.mainView.getContext(), "Dragged data is " + dragData, Toast.LENGTH_LONG);

	                    // Turns off any color tints
	                    v.setScaleX(1);
	                    v.setScaleY(1);
	                    int key=0;
	                    if (dragData.toString().contentEquals("A")){
	                    	key=1;
	                    	
	                    }else{
	                    	if (dragData.toString().contentEquals("B"))
	                    	{
	                    		key=2;
	                    	}
	                    	else
	                    	{
	                    		if (dragData.toString().contentEquals("P"))
		                    	{
		                    		key=3;
		                    	}	
	                    	}
	                    	
	                    }
	                    	
	                    
	                    //node.changeImage(key);
	                    //node.changeText(dragData.toString());
	                    node.setGenericRepresentation(dragData.toString());
	                    //node.imageView.setImageBitmap(dragData.)
	                    //node.refreshCurrentRepresentation();
	                    node.startRefreshWave();
	                    //gr.changeImage(what,key);
	                    //((Ball) v).mPaint.setColor(0xFFFF00FF);
	                    // Invalidates the view to force a redraw
	                    v.invalidate();

	                    // Returns true. DragEvent.getResult() will return true.
	                    return(true);
	                }
	                //break;

	                case DragEvent.ACTION_DRAG_ENDED:
	                {
	                    // Turns off any color tinting
	                	v.setScaleX(1);
	                    v.setScaleY(1);

	                   

	                    // Does a getResult(), and displays what happened.
	                    if (event.getResult()) {
	                    	
	                    	
	                    	//View parent= (View) v.getParent();
	                        //Toast.makeText( parent.getContext(), "The drop was handled.", Toast.LENGTH_LONG);
	                    	
	                    } else {
	                       Toast.makeText(v.getContext(), "The drop didn't work.", Toast.LENGTH_LONG) ;
	                       //gr.changeImage(what, 3);
	                    };
	                    // Invalidates the view to force a redraw
	                    v.invalidate();
	                    // returns true; the value is ignored.
	                    return(true);
	                }
	                //break;

	                // An unknown action type was received.
	                default:
	                {
	                	Log.e("DragDrop Example","Unknown action type received by OnDragListener.");
	                	return false;
	                    
	                }
	                //break;
	        }
			//return false;
	    };
	};

